<template>
  <view class="collect">
    <scroll-view :scroll-y="true" @scrolltolower="scrolltolower">
      <div class="list" v-if="list.length > 0">
        <div class="block" v-for="(item, index) in list" :key="index">
          <div class="left" @click="onDetail(item.id)">
            <u-image
              height="186rpx"
              width="148rpx"
              :src="item.cover"
              radius="10rpx"
            ></u-image>
          </div>
          <div class="center" @click="onDetail(item.id)">
            <div class="name">{{ item.name }}</div>
            <div class="tag">讲师：{{ item.teacher }}</div>
            <div class="tag">开始时间：{{ item.start_date }}</div>
            <div class="tag">结束时间：{{ item.end_date }}</div>
          </div>
          <div class="right">
            <u-button
              type="error"
              size="mini"
              text="取消报名"
              @click="oncancelCollect(item.id)"
            >
            </u-button>
          </div>
        </div>
      </div>
      <u-empty v-else mode="order" iconSize="120" text="数据为空"> </u-empty>
    </scroll-view>
  </view>
</template>

<script>
import { cancelSign, getUserSign } from "apis/modules/user";
import { getStorage } from "utils/auth";
export default {
  name: "collect",

  data() {
    return {
      page: 1,
      isMore: false,
      list: [],
    };
  },

  onLaunch() {},
  onLoad() {
    this.page = 1;
    this.courseList();
  },
  onShow() {},
  onHide() {},
  methods: {
    oncancelCollect(course_id) {
      cancelSign({ course_id }).then((res) => {
        uni.showToast({
          title: "已取消收藏",
          duration: 2000,
        });
        this.page = 1;
        this.courseList();
      });
    },
    scrolltolower() {
      if (this.isMore) {
        return false;
      }
      this.page++;
      this.courseList();
    },
    onDetail(course_id) {
      let token = !!getStorage("token");
      if (token) {
        this.$Router.push({
          path: `/pagesA/offlineDetail/offlineDetail?course_id=${course_id}`,
        });
      } else {
        this.show = true;
      }
    },
    courseList() {
      let data = {
        page: this.page,
      };
      getUserSign(data).then((result) => {
        console.log(result);
        if (data.page == 1) {
          this.list = result;
        } else {
          this.list = this.list.concat(result);
        }
        this.isMore = result.length <= 0;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.collect {
  background: #eeeeee;
  border-radius: 0rpx 0rpx 0rpx 0rpx;
  opacity: 1;
  min-height: 100vh;
  .block {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    background: #fff;
    padding: 20rpx;
    margin: 20rpx;
    border-radius: 20rpx;
    .left {
      flex-shrink: 0;
      margin-right: 20rpx;
    }
    .center {
      flex: 1;
      font-size: 24rpx;
      color: #333333;
      .name {
        font-size: 32rpx;
        font-weight: bold;
        color: #333333;
        margin-bottom: 20rpx;
      }
    }
    .right {
      flex-shrink: 0;
      padding: 0 20rpx;
    }
  }
}
</style>
